﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using CMS.Presenters;
using CMS.Presenters.Presenters.Template;

namespace CMS.Web.Common
{
    public partial class LeftMenu : ViewUserControl, IMenuView
    {
        LeftMenuPresenter _presenter = null;        

        protected void Page_Load(object sender, EventArgs e)
        {
            this._presenter = new LeftMenuPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietTheLoaiDao());
            ProcessMenuView();
        }

        private void ProcessMenuView()
        {
            CMS.Web.Common.VBound ctlIB = null;
            object childId = null;
            List<BaiVietTheLoai> results2 = null;
            List<BaiVietTheLoai> lstNoParent = null;
            List <BaiVietTheLoai> menus = _presenter.GetMenuView();
            lstNoParent = menus.FindAll(delegate(BaiVietTheLoai bv) { return bv.TheLoaiCha != null; });
            if ((menus != null) && (menus.Count > 0))
            {
                foreach (BaiVietTheLoai entity in menus)
                {
                    if (entity.TheLoaiCha ==null)// parent group
                    {
                        //results2 = menus.FindAll(delegate(BaiVietTheLoai bv) { return bv.TheLoaiCha == null; });
                        results2 = lstNoParent.FindAll(delegate(BaiVietTheLoai bv) { return bv.TheLoaiCha.ID == entity.ID; });
                        results2.Sort(delegate(BaiVietTheLoai v1, BaiVietTheLoai v2) { return v1.ThuTu.CompareTo(v2.ThuTu); });
                        // get id menu first
                        if (results2.Count > 0) childId = results2[0].ID;
                        ctlIB = (CMS.Web.Common.VBound)Page.LoadControl("Common/Menu/VBound.ascx");
                        ctlIB.Init_VBound(entity.ID.ToString(), entity.TenTheLoai, false, false, childId);
                        ctlIB.MenuSubs = results2;
                        ctlIB.AllMenus = menus;
                        this.ulMenu.Controls.Add(ctlIB);                       
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
    }
}